namespace mplat {
    export class WxInterstitialAd extends InterstitialAd {

        protected _interstitialAd: any;

        protected _readyShowInterstitial = false;

        protected _closeFun: () => void;

        showInterstitialAd(close?: () => void, rate = 1) {
            if (plat.ad.state == AdState.NoAd) {
                debugLog('广告状态设置为了未上线状态，所以不显示')
                return;
            }
            if (!this._canUseInterstitial()) return;
            if (Math.random() > rate) return;
            this._closeFun = close;
            this._readyShowInterstitial = true;
            // 因为一创建就会去拉取，所以这里要加上全局预加载
            if (!!this._info.preload) this._info.preload();
            if (!this._interstitialAd) {
                this._interstitialAd = this._getInterstitialAd();
            } else {
                this.loadInterstitial();
            }
        }
        loadInterstitial() {
            if (!this._canUseInterstitial()) return;
            this._interstitialAd = this._getInterstitialAd();
            this._interstitialAd.load();
        }

        protected _canUseInterstitial() {
            let can = !!wx['createInterstitialAd'];
            if (!can) {
                debugLog('当前宿主不支持插屏')
            }
            return can;
        }

        protected _destroy() {
            if (!this._interstitialAd) return;
            this._interstitialAd.offError(this._onErr.bind(this))
            this._interstitialAd.offLoad(this._onLoaded.bind(this))
            this._interstitialAd.offClose(this._onClose.bind(this))
            if (!!this._interstitialAd['destroy']) this._interstitialAd['destroy']()
            this._interstitialAd = null;
        }

        protected _getInterstitialAd(id?: string) {
            if (!this._interstitialAd) {
                this._interstitialAd = wx['createInterstitialAd']({
                    adUnitId: id || this._info.ids[0]
                });
                this._interstitialAd.onError(this._onErr.bind(this))
                this._interstitialAd.onLoad(this._onLoaded.bind(this))
                this._interstitialAd.onClose(this._onClose.bind(this))
            }
            return this._interstitialAd;
        }
        protected _onErr(err) {
            debugLog('插屏拉取失败', err)
            if (!!this._info.onErr) this._info.onErr(err);
            this._readyShowInterstitial = false;
        }
        protected _onLoaded() {
            debugLog('插屏拉取成功')
            if (!!this._info.loadSuccess) this._info.loadSuccess();
            this._hasInterstitial = true;
            if (!this._readyShowInterstitial) {
                return;
            }
            this._showInter()
        }
        protected _showInter() {
            this._interstitialAd.show().then(() => {
                this._readyShowInterstitial = false;
                debugLog('插屏显示成功')
                if (!!this._info.shown) this._info.shown();
            }).catch((err) => {
                debugLog('插屏显示失败')
            })
        }
        protected _onClose(res) {
            debugLog('插屏关闭')
            if (!!this._closeFun) this._closeFun();
            this._closeFun = null;
            // 由于wx每次都是创建一个全新的插屏广告，所以这里关闭的时候直接销毁
            this._destroy();
        }
    }
}